<?php
/***
 * 发行人
 */
namespace app\api\controller;

use app\api\controller\BaseController;

class OrganController extends BaseController{
	/**
	*
    *列表
	**/
	public function getList(){
        $param		= input('post.');
        $token			= (input('post.token')) ? input('post.token') : '';
		$where = 'a.state=1 and a.user_type = 4 and b.status=2';
		if($token){
			$userArr	= explode(',',auth_code($token,'DECODE'));
			//$uid		= $userArr[0];
			//$where.=" and a.uid=$uid";

		}
		//每页记录数
		$pageSize	= (isset($param['pagesize']) and $param['pagesize']) ? $param['pagesize'] : 10;
		//当前页
		$page		= (isset($param['page']) and $param['page']) ? $param['page'] : 1;

        $data	= model('Users')->alias('a')
            ->field('a.username,a.nickname,a.header,a.photo_wall,b.content,b.create_time,b.id,b.uid')
            ->join('users_organ b','a.id=b.uid','left')
            ->where($where)
            ->order('b.create_time desc')
            ->paginate($pageSize)
            ->toArray();

		foreach ($data['data'] as $key => &$value) {
			$value['header']=$value['header']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$value['header']):'';
            $value['photo_wall']=$value['photo_wall']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$value['photo_wall']):'';
            $value['create_time']=date('Y-m-d H:i:s',$value['create_time']);
		}
		return json(['code'=>1,'data'=>$data]);
	}
    /**
	*
    *详情
	**/
	public function getDetail(){
        $lang			= (input('post.lang')) ? input('post.lang') : 'en';	// 语言类型
		$id		= input('post.id');
        if(empty($id)){
            return json(['code'=>0,'code_dec'=>'参数错误']);
        }
        $token			= (input('post.token')) ? input('post.token') : '';

        $where = 'a.state=1 and a.user_type = 4 and b.status=2 and b.id='.$id;

		if($token){
			$userArr	= explode(',',auth_code($token,'DECODE'));
			//$uid		= $userArr[0];
			//$where.=" and a.uid=$uid";
		}
        $data	= db('Users')->alias('a')
        ->field('a.username,a.nickname,a.header,a.photo_wall,b.*')
        ->join('users_organ b','a.id=b.uid','left')
        ->where($where)
        ->find();


        if(empty($data)){
            return json(['code'=>0,'code_dec'=>config($lang.'.lang2')[6]]);
        }
        $statusArr=model('Organ')->getStatus();
		$data['header']=$data['header']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$data['header']):'';
        $data['photo_wall']=$data['photo_wall']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$data['photo_wall']):'';
        $data['create_time']=date('Y-m-d H:i:s',$data['create_time']);
        $data['statusStr']=$statusArr[$data['status']];

		return json(['code'=>1,'data'=>$data]);
	}
	/**
	*
    *申请
	**/
	public function apply(){
        $lang			= (input('post.lang')) ? input('post.lang') : 'en';	// 语言类型
        $token			= request()->param('token','','trim') ;
        $param = input('param.');
        $data['uid']=0;
		if($token){
			$userArr = explode(',',auth_code($token,'DECODE'));

			$data['uid'] = $userArr[0];
		}
        if (!isset($param['business'])||!isset($param['card_up'])||!isset($param['card_down'])) {
            return ['code'=>0,'code_dec'=>config($lang.'.lang2')[2]];
        }

        $data['content'] = request() ->param('content','','trim');
        $data['country_name'] = request() ->param('country_name','','trim');
        $data['company_name'] = request() ->param('company_name','','trim');
        $bus_screenshot = '';
        if (isset($param['business'])) {
            $imgcontent = $param['business'][0]["content"];
            $screenFile = base64_image_content($imgcontent, "./upload/image");
            if ($screenFile) {
                $bus_screenshot = $screenFile;
            }else{
                return ['code'=>0,'code_dec'=>config($lang.'.lang2')[2]];
            }
        }
        $card_up_screenshot = '';
        if (isset($param['card_up'])) {
            $imgcontent = $param['card_up'][0]["content"];
            $screenFile = base64_image_content($imgcontent, "./upload/image");
            if ($screenFile) {
                $card_up_screenshot = $screenFile;
            }else{
                return ['code'=>0,'code_dec'=>config($lang.'.lang2')[2]];
            }
        }
        $card_down_screenshot = '';
        if (isset($param['card_down'])) {
            $imgcontent = $param['card_down'][0]["content"];
            $screenFile = base64_image_content($imgcontent, "./upload/image");
            if ($screenFile) {
                $card_down_screenshot = $screenFile;
            }else{
                return ['code'=>0,'code_dec'=>config($lang.'.lang2')[2]];
            }
        }

        $data['business'] = $bus_screenshot;
        $data['card_up'] = $card_up_screenshot;
        $data['card_down'] = $card_down_screenshot;

        //数据验证

		$validate = validate('app\api\validate\Organ');
		if(!$validate->scene('add')->check($data)){
			//抛出异常
			return json(['code'=>0,'code_dec'=>$validate->getError()]);
		}
        $data['create_time']=time();
        $data['status']=1;

        $info	= db('Users_organ')->alias('a')
        ->where(['uid'=>$data['uid']])
        ->find();
        if($info){
            model('Organ')->where(['id'=>$info['id']])->update($data);
        }else{
            model('Organ')->save($data);
        }
		return json(['code'=>1,'data'=>[],'code_dec'=>config($lang.'.lang2')[3]]);
	}
    /**
	*
    *详情
	**/
	public function getApplyDetail(){
        $lang			= (input('post.lang')) ? input('post.lang') : 'en';	// 语言类型

        $token			= (input('post.token')) ? input('post.token') : '';

        if(!$token){
            return json(['code'=>0,'code_dec'=>'用户信息不存在']);
        }
		$where = '1=1';

		if($token){
			$userArr	= explode(',',auth_code($token,'DECODE'));
			$uid		= $userArr[0];
			$where.=" and a.uid=$uid";
		}
        $data	= db('Users_organ')->alias('a')
        ->field('a.*')
        ->where($where)
        ->find();
       //p($where);

        if(empty($data)){
            return json(['code'=>0,'code_dec'=>config($lang.'.lang2')[6]]);
        }
        $statusArr=model('Organ')->getStatus();

		$data['business']=$data['business']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$data['business']):'';
        $data['card_up']=$data['card_up']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$data['card_up']):'';
        $data['card_down']=$data['card_down']?($_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$data['card_down']):'';
        $data['create_time']=date('Y-m-d H:i:s',$data['create_time']);
        $data['statusStr']=$statusArr[$data['status']];

		return json(['code'=>1,'data'=>$data]);
	}




}
